package TestVolatile;

import java.util.Scanner;

/**
 * @author tongchen
 * @create 2023-06-15 23:22
 */
public class VolatileTest {
    public volatile static int num=0;

    public static void main(String[] args) throws InterruptedException {
        Scanner scanner=new Scanner(System.in);
         Thread thread1 =new Thread(()->{
             System.out.println("线程1循环开始");
             try {

                 Thread.sleep(2000);
             } catch (InterruptedException e) {
                 throw new RuntimeException(e);
             }
             while(num==0){
                        
                    }
             System.out.println("线程1循环结束.......");
                 });
        thread1.start();
       // Thread.sleep(2000);
        Thread thread2 =new Thread(()->{
              System.out.println("线程2开始");
                  num=scanner.nextInt();
              System.out.println("线程2执行结束");
                  });

          thread2.start();
          thread1.join();
          thread2.join();
    }
}
